package com.yueshop.seckill.controller;


import com.yueshop.common.utils.R;
import com.yueshop.seckill.service.SeckillService;
import com.yueshop.seckill.vo.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getcurrentSeckillSkus(){
        log.info("currentSeckillSkus正在执行...........");
        List<SeckillSkuRedisTo> data = seckillService.getcurrentSeckillSkus();
        return R.ok().setData(data);
    }


    //根据skuId获取秒杀商品信息
    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        SeckillSkuRedisTo skuRedisTo =  seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(skuRedisTo);
    }
    //"http://seckill.yueshop.com/seckill?killId=" + killId + "&key=" + key + "&num=" + num;


    @GetMapping("/seckill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num, Model model){
        String orderSn =  seckillService.seckill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }


}
